package com.hhs.storage.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.hhs.storage.bean.*;
import com.hhs.storage.dao.JxcProductOrderDao;
import com.hhs.storage.service.JxcProductOrderService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.hhs.storage.service.JxcProductService;
import com.hhs.storage.service.JxcTypeService;
import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author shadow
 * @since 2018-12-18
 */
@Service
public class JxcProductOrderServiceImpl extends ServiceImpl<JxcProductOrderDao, JxcProductOrder> implements JxcProductOrderService {

    @Resource
    JxcProductService jxcProductService;

    @Override
    public Map<String, Object> edit(JxcProductOrder jxcProductOrder) {

        User user = (User)SecurityUtils.getSubject().getSession().getAttribute("user");
        jxcProductOrder.setCreateDate(new Date());
        jxcProductOrder.setCreater(user.getName());
        jxcProductOrder.setCreaterId(user.getId());
        jxcProductOrder.setStatus(1);//在途
        JxcProduct product = jxcProductService.selectOne(new EntityWrapper<JxcProduct>().eq("type_id_",jxcProductOrder.getTypeId()));

        if (product == null) {//产品不存在
            //无法出库
            if(2 == jxcProductOrder.getOrderType()){
                return Result.failJsonResp("库存不足，无法创建单据");
            }
        }else{//产品存在
            if(2 == jxcProductOrder.getOrderType()){
                if(product.getTotal()<jxcProductOrder.getNumber()){//库存小于出库量
                    return 	Result.failJsonResp("库存不足，无法创建单据");
                }
            }
        }
        this.insert(jxcProductOrder);


        return Result.sucJsonResp(jxcProductOrder);
    }

    @Override
    public Map<String, Object> operateOrder(boolean agreeFlag, String orderId, String remark) {
        JxcProductOrder order =this.selectById(orderId);
        JxcProduct product = jxcProductService.selectOne(new EntityWrapper<JxcProduct>().eq("type_id_",order.getTypeId()));
        if(agreeFlag){//同意
            order.setStatus(3);
            if (product == null) {//产品不存在
                //无法出库
                if(2 == order.getOrderType()){
                    order.setStatus(2);
                    order.setRemark("产品库存不足，待补足后再申请");
                    return Result.failJsonResp("库存不足，无法出库");
                }else{
                    //直接新建一个产品统计数据
                    product = new JxcProduct();
                    product.setTotal(order.getNumber());
                    product.setTypeId(order.getTypeId());
                    jxcProductService.insert(product);
                }
            }else{//产品存在
                if(2 == order.getOrderType()){
                    if(product.getTotal()<order.getNumber()){//库存小于出库量
                        order.setStatus(2);
                        order.setRemark("产品库存不足，待补足后再申请");
                        return 	Result.failJsonResp("库存不足，无法出库");
                    }else{
                        product.setTotal(product.getTotal()-order.getNumber());
                    }
                }else{
                    product.setTotal(product.getTotal()+order.getNumber());
                }
                jxcProductService.insertOrUpdate(product);
            }
        }else{//驳回
            order.setStatus(2);
            order.setRemark(remark);
        }

        this.insertOrUpdate(order);
        return Result.sucJsonResp(null);
    }

    @Override
    public Page<JxcProductOrder> findPage(InputParam inputParam) {
        Page<JxcProductOrder> page = new Page<>(inputParam.getPage(),inputParam.getSize());// 当前页，总条数 构造 page 对象
        return page.setRecords(this.baseMapper.findPage(page,inputParam));
    }
}
